<?php
Class rchatModel extends AppModel{
	public function  listUnchecked($page,$cfg){
		$pageSize = $cfg['capacityPooleach'];//每页数量
		$listPage=array('data'=>'','pageLink'=>'');
		$where = " p1.status in(1,2) order by p1.status ,  p1.addtime desc ";
		$result=$this->get_page(" p1.* ,  p2.nickname ",$this->table('chat_record')."  p1 LEFT JOIN  ".$this->table('wxusers')."  p2 ON p1.wxid=p2.wxid ",$where,$page,$pageSize);
		$digestTime = $cfg['digestTime'];
		foreach($result as $k=>$v){
			switch ($v['status']){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过 6:经过审核未审核7审核中未经过审核的
				case 0:$result[$k]['statusShow'] = "未审核";break;
				case 1:$result[$k]['statusShow'] = "审核中";break;
				case 2:$result[$k]['statusShow'] = "审核不通过";break;
				case 3:$result[$k]['statusShow'] = "审核通过";break;
				case 4:$result[$k]['statusShow'] = "审核通过";break;
				case 5:$result[$k]['statusShow'] = "审核不通过";break;
				case 6:$result[$k]['statusShow'] = "未经过审核";break;
				case 7:$result[$k]['statusShow'] = "未经过审核";break;
			}
			//如果该时间戳超过24小时，则失效
			if($v['addStatus']==1){//加精评论
				if(time()-$v['addDigestTime']>($digestTime*86400)){
					$result[$k]['addStatus'] =0;
					$result[$k]['addDigestTime'] =0;
				}
			}
		}
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where p1.status in(1,2) ";
		$count=$this->getOne($sql);
		//获取审核中通过的数据
		$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where p1.status=3 ";
		$passCount=$this->getOne($sql);
		$chatPool = $cfg["refreshtime"];//审核池最大数==刷新时间
		if($passCount>$chatPool){
			$passCount = $chatPool;
		}
		$listPage['count']=$count;
		$listPage['passCount']=$passCount;
		return $listPage;
	}
	
	public function get_uv_pv_2M($now,$time,$refresh){
		$start = $now - ($now-$time)%$refresh;
		$end = $now+ ($refresh-($now-$time)%$refresh);
		$sql = "SELECT count(*) FROM ".$this->table('chat_visitor')." WHERE logintime>=$start AND logintime<=$end ";
		$pv = $this->getOne($sql);
		$sql = "SELECT  count(*) FROM ".$this->table('chat_visitor')." WHERE logintime>=$start AND logintime<=$end  GROUP BY openid ";
		$uv = $this->getOne($sql);
		$result['pv'] = $pv;
		$result['uv'] = $uv;
		return $result;
	}
	
	public function updateStatus($uids,$status){
		if(!$uids){
			return false;
		}
		$update = time();
		$sql = "update ".$this->table('chat_record')." SET status =".$status." , updatetime=".$update." WHERE id in (".$uids.")";
		return $this->query($sql);
	}
	
	public function cleanChatPool(){
		$sql = "UPDATE  ".$this->table("options")." SET op_value= 0 WHERE op_key='lastCycleNum'";
		$this->query($sql);
	}
	
	public function updateComment()
	{
		$update = yf_time();
		$cfg = $this->get_cfg();
		$isOrderBy = $cfg['isOrderBy'];
		$chatPool = $cfg["refreshtime"];//新版中，弹幕池总数==刷新时间
		$sql = "SELECT count(*) FROM ".$this->table('chat_record')." WHERE status=3 ";//审核中审核通过的数量
		$count=$this->getOne($sql);
		if($chatPool<$count){//总数大于chatPool
			if($isOrderBy){//顺序
				$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." as p1 , ".$this->table('wxusers')."  as p2 WHERE p1.status=3 and  p1.wxid=p2.wxid  order by  p1.addtime desc LIMIT $chatPool";
			}else{
				$sql = "SELECT id FROM ".$this->table('chat_record')."  WHERE status=3 order by addtime desc";
				$passAll = $this->getAll($sql);
				$num = intval($count/$chatPool);
				$ids = "";
				for($i=0;$i<=$count-1;$i=$i+$num){
					$ids.=$passAll[$i]['id'].",";
				}
				$ids=substr($ids,0,strlen($ids)-1);
				$sql ="SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')."  p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid=p2.wxid WHERE p1.id  in ( ".$ids.") AND p1.status=3 order by  p1.addtime desc LIMIT $chatPool";
			}
		}else{
			$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." p1 LEFT JOIN  ".$this->table('wxusers')."  p2 ON p1.wxid=p2.wxid  WHERE p1.status=3  order by  p1.addtime desc LIMIT $chatPool";
		}
		$result = $this->getAll($sql);
		$res['result'] = $result;
		$sql = "SELECT comment FROM ".$this->table("chat_record")." WHERE status = 8 ORDER BY addtime ASC ";
		$immediately = $this->getAll($sql);
		$res['immediately'] = $immediately;
		$count = count($result);
		$res['count']=$count;
		$this->data2json($res,$cfg);
		
		//获取容量池的大小
		$capacityPool = $cfg['capacityPool'];
		$sql = "UPDATE ".$this->table('chat_record')." SET status=6  , updatetime=".$update." WHERE status = 1  ";
		$this->query($sql);//将审核中的改为未操作的
		
		$sql = "UPDATE ".$this->table('chat_record')." SET status=1  , updatetime=".$update." WHERE status = 0  ";
		$ountSql = "SELECT count(*) FROM ".$this->table("chat_record")." WHERE status=0";
		$count = $this->getOne($ountSql);
		if($count){
			if($isOrderBy){//顺序
				$order = " SELECT id FROM ".$this->table('chat_record')." WHERE status=0  order by addtime desc LIMIT ".$capacityPool;
				$all = $this->getAll($order);
				for($i=0;$i<count($all);$i++){
					$ids.=$all[$i]['id'].",";
				}
				$ids=substr($ids,0,strlen($ids)-1);
				$sql = $sql." AND id in ( ".$ids.")";//将未审核的变成审核中
			}else{//否
				if($count<=$capacityPool){//未审核数量少
					$order = " AND 1=1";
					$sql = $sql.$order;
				}else{//数量大
					$allSql = "SELECT id FROM ".$this->table('chat_record')." WHERE status=0 order by addtime desc ";
					$all = $this->getAll($allSql);
					$num = intval($count/$capacityPool);
					$ids = "";
					for($i=0;$i<=$count-1;$i=$i+$num){
						$ids.=$all[$i]['id'].",";
					}
					$ids=substr($ids,0,strlen($ids)-1);
					$sql = $sql." AND id in ( ".$ids.")";//将未审核的变成审核中
				}
			}
			$this->query($sql);
			
		}
		$sql = " UPDATE ".$this->table("chat_record")." SET updatetime = $update, `status` = CASE ".
				//" WHEN status = 1 THEN 6 ".//将审核中的改为未操作的
				" WHEN status = 3 THEN 4 ".//将审核中通过的变为通过
				" WHEN status = 2 THEN 5 ".//将审核中不通过的变为不通过
				" WHEN status = 0 THEN 6 ".//超出容量池的改成未审核的
				" WHEN status = 8 THEN 9 ".//将立即对话改为已审核过的
				" ELSE status END ";
		$this->query($sql);
	}
	public function updateCommentPass()
	{
		$update = yf_time();
		//加上一个旁路功能，即把未审核的改为审核通过
		$sql = "update ".$this->table('chat_record')." SET status =3 , updatetime=".$update." WHERE status = 1";
		$this->query($sql);
		$cfg = $this->get_cfg();
		$isOrderBy = $cfg['isOrderBy'];
		$chatPool = $cfg["refreshtime"];//新版中，弹幕池总数==刷新时间
		$sql = "SELECT count(*) FROM ".$this->table('chat_record')." WHERE status=3 ";
		$count=$this->getOne($sql);
			
		if($chatPool<$count){
			if($isOrderBy){//顺序
				$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')."  p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid=p2.wxid WHERE p1.status=3 order by  p1.addtime desc LIMIT $chatPool";
			}else{
				$sql = "SELECT id FROM ".$this->table('chat_record')."  WHERE status=3 order by addtime desc";
				$passAll = $this->getAll($sql);
				$num = intval($count/$chatPool);
				$ids = "";
				for($i=0;$i<=$count-1;$i=$i+$num){
					$ids.=$passAll[$i]['id'].",";
				}
				$ids=substr($ids,0,strlen($ids)-1);
				$sql ="SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid=p2.wxid  WHERE p1.id  in ( ".$ids.") AND p1.status=3  order by  p1.addtime desc LIMIT $chatPool";
			}
		}else{
			$sql = "SELECT p1.*,p2.headimgurl FROM ".$this->table('chat_record')." p1 LEFT JOIN ".$this->table('wxusers')."  p2 ON p1.wxid=p2.wxid WHERE p1.status=3  order by  p1.addtime desc LIMIT $chatPool";
		}
		$result = $this->getAll($sql);
		$res['result'] = $result;
		$sql = "SELECT comment FROM ".$this->table("chat_record")." WHERE status = 8 ORDER BY addtime ASC ";
		$immediately = $this->getAll($sql);
		$res['immediately'] = $immediately;
		$count = count($result);
		$res['count']=$count;
		$this->data2json($res,$cfg);
		
		$sql = "UPDATE ".$this->table('chat_record')." SET status=6  , updatetime=".$update." WHERE status = 1  ";
		$this->query($sql);//将审核中的改为未操作的
		//获取容量池的大小
		$capacityPool = $cfg['capacityPool'];
		$sql = "update ".$this->table('chat_record')." SET status=1  , updatetime=".$update." WHERE status = 0  ";
		$ountSql = "SELECT count(*) FROM ".$this->table("chat_record")." WHERE status=0";
		$count = $this->getOne($ountSql);
		if($count)
		{
			if($isOrderBy){//顺序
				$order = " SELECT id FROM ".$this->table('chat_record')." WHERE status=0  order by addtime desc   LIMIT ".$capacityPool;
				$all = $this->getAll($order);
				for($i=0;$i<count($all);$i++){
					$ids.=$all[$i]['id'].",";
				}
				$ids=substr($ids,0,strlen($ids)-1);
				$sql = $sql."AND id in ( ".$ids.")";//将未审核的变成审核中
			}else{//否
				if ($count <= $capacityPool) { // 未审核数量少
					$order = " AND 1=1";
					$sql = $sql . $order;
				} else { // 数量大
					$allSql = "SELECT id FROM " . $this->table ( 'chat_record' ) . " WHERE  status=0 order by addtime desc";
					$all = $this->getAll ( $allSql );
					$num = intval ( $count / $capacityPool );
					$ids = "";
					for($i = 0; $i <= $capacityPool - 1; $i = $i + $num) {
						$ids .= $all [$i] ['id'] . ",";
					}
					$ids = substr ( $ids, 0, strlen ( $ids ) - 1 );
					$sql = $sql . "AND id in ( " . $ids . ")"; // 将未审核的变成审核中
				}
			}
			$this->query($sql);
		}
		$sql = " UPDATE ".$this->table("chat_record")." SET updatetime = $update, `status` = CASE ".
				" WHEN status = 3 THEN 4 ".//将审核中通过的变为通过
				" WHEN status = 2 THEN 5 ".//将审核中不通过的变为不通过
				" WHEN status = 0 THEN 6 ".//超出容量池的改成未审核的
				" WHEN status = 8 THEN 9 ".//将立即对话改为已审核过的
				" ELSE status END ";
		$this->query($sql);
		
	}
	function data2json($res,$cfg)
	{
		if((count($res['result'])+count($res['immediately']))==0){//为空
			$isRepeat = intval($cfg["isRepeat"]);
			if($isRepeat==1){
				return ;
			}
		}
		$refresh = intval($cfg['refreshtime']);
		$result=array();
		for($temp=0;$temp<$refresh;$temp++)
		{
			$immediately =  $res['immediately'][$temp];
			if($immediately)
			{
				$im = array();
				$im['comment'] = $immediately['comment'];
				$comm['headimgurl'] = "http://g4cdn.airefp.com/ws/Mobile/Webroot/gf/images/chat/share.png";
				$im['status'] = "immediately";
				$result[] = $im;
				if(count($result)>=$refresh){
					break;
				}
			}
			$comment =  $res['result'][$temp];
			if($comment)
			{
				$comm = array();
				$comm['wxid'] = $comment['wxid'];
				if(preg_match("/\/0$/", $comment['headimgurl'])){
					$comment['headimgurl']=substr($comment['headimgurl'],0,strlen($comment['headimgurl'])-1)."64";
				}
				$comm['headimgurl'] = $comment['headimgurl'];
				$comm['comment'] = $comment['comment'];
				$comm['status'] = "comment";
				$result[] = $comm;
				if(count($result)>=$refresh){
					break;
				}
			}
		}
		$sql = "UPDATE  ".$this->table("options")." SET op_value= ".count($result)." WHERE op_key='lastCycleNum'";
		$this->query($sql);
		$fp = fopen(ROOT_PATH."chatR.json", "w");
		fwrite($fp, json_encode($result));
		fclose($fp);
	}
	
	public function update_pass_status($status)
	{
		$sql="update ".$this->table('options')." SET op_value= ".$status." WHERE op_key='passstatus'";
		$this->query($sql);
		$status = $this->get_status_pass();
		return  $status;
	}
	public function clearNopass()
	{
		$result=array('error'=>0,'content'=>'删除成功');
		$sql="DELETE FROM ".$this->table('chat_record')." WHERE  status=6 ";
		$res=$this->query($sql);
		if(!$res){
			$result['error']=1;
			$result['content']='删除失败';
		}
		return $result;
	}
	public function silenceUser($wxid,$status)
	{
		$time =yf_time();
		$sql = " SELECT count(*) FROM ".$this->table('chat_silence_user')." WHERE wxid=$wxid AND endTime>=".$time;
		$num = $this->getOne($sql);
		if($num){//有值修改
			$sql = " SELECT id FROM ".$this->table('chat_silence_user')." WHERE wxid=$wxid AND endTime>=".$time." LIMIT 1";
			$id = $this->getOne($sql);
			$data['addTime'] = $time;
			$data['status'] = $status;
			$data['wxid'] = $wxid;
			if($status==1){//小时
				$data['endTime'] = $time+3600;
			}else{
				$data['endTime'] = strtotime('+5 day',$time);
			}
			$res = $this->update($data,array("table"=>"chat_silence_user","id"=>$id));
	
		}else{//新增
			$data['addTime'] = $time;
			$data['status'] = $status;
			$data['wxid'] = $wxid;
			if($status==1){//小时
				$data['endTime'] = $time+3600;
			}else{
				$data['endTime'] = strtotime('+5 day',$time);
			}
			$res = $this->insert($data, "chat_silence_user");
		}
		if($res){
			return true;
		}else{
			return false;
		}
	}
	public function del_record($id)
	{
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
			$sql="DELETE FROM ".$this->table('chat_record')." WHERE  id='".$id."'";
			$res=$this->query($sql);
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
		}
		return $result;
	}
	
	public function addDigest($id)
	{
		$result=array('error'=>0,'content'=>'加精成功');
		$time = yf_time();
		$digestTime = $this->get_cfg('digestTime');
		$sql ="SELECT addStatus FROM ".$this->table('chat_record')." WHERE id=$id";
			
		$status = $this->getOne($sql);
		if($status==0){
			$sql ="UPDATE ".$this->table('chat_record')." SET addStatus=1,addDigestTime=".$time." WHERE id=$id";
		}else{
			//原先加精的二次加精
			$sql ="SELECT addDigestTime FROM ".$this->table('chat_record')." WHERE id=$id";
			$addDigestTime = $this->getOne($sql);
			if($time-$addDigestTime>($digestTime*86400)){//已失效的二次加精
				$sql ="UPDATE ".$this->table('chat_record')." SET addStatus=1,addDigestTime=".$time." WHERE id=$id";
			}else{
				$sql ="UPDATE ".$this->table('chat_record')." SET addStatus=0,addDigestTime=".$time." WHERE id=$id";
			}
		}
		$res=$this->query($sql);
		if(!$res){
			$result['error']=1;
			$result['content']='加精失败';
		}
		return $result;
	}
	
	public function addDigestPool()
	{
		$digestNum = $this->get_cfg('digestNum');
		$digestTime = $this->get_cfg('digestTime');
		$time = yf_time();
		$sql = "SELECT id FROM ".$this->table('chat_record')." WHERE addstatus=1 and addDigestTime>=".($time-($digestTime*86400))."   order by  addDigestTime desc LIMIT $digestNum ";
		$res = $this->getAll($sql);
		$ids = '';
		if(count($res)==0){
			$result = array('error'=>1,'content'=>'无有效加精');
			return $result;
		}
		foreach ($res as $k=>$v){
			$ids=$ids.$v['id'].",";
		}
		$ids=substr($ids,0,strlen($ids)-1);
		$resu=$this->updateStatus($ids,1);
		if($resu){
			$result=array('error'=>0,'content'=>'成功添加至弹幕池');
		}else{
			$result = array('error'=>1,'content'=>'添加至弹幕池失败');
		}
		return $result;
	}
	
	public function get_status_pass()
	{
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='passstatus'";
		$is_appid=$this->getOne($sql);
		return  $is_appid;
	}
	public function saveConfig($info)
	{
		foreach ($info as $k=>$v)
		{
			$sql = "UPDATE ".$this->table('options')." SET op_value='$v'"." WHERE op_key='$k'";
			$res = $this->query($sql);
		}
		return $res;
	}
	
	public function silence_list($page)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where = "  1=1  order by  p1.addtime desc ";
		$result=$this->get_page(" p1.* ,  p2.nickname ",$this->table('chat_silence_user')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid=p2.wxid ",$where,$page,'50');
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('chat_silence_user')."  as p1 ";
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	
	public function del($id,$table)
	{
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
			$sql="DELETE FROM ".$this->table($table)." WHERE  id='".$id."'";
			$res=$this->query($sql);
			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
		}
		return $result;
	}
	
	public function  special_list($page){
		$listPage=array('data'=>'','pageLink'=>'');
		$where = " 1=1  order by  p1.addtime desc ";
		$result=$this->get_page(" p1.*,p2.name as group_name,p3.title as hongbao_name ",$this->table('chat_special')." as p1 left join ".$this->table('box_group')." as p2 on p2.id = p1.group_id left join ".$this->table('lucky_info')." as p3 on p3.id = p1.hongbao_id ",$where,$page,'20');
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('chat_special');
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	
	public function get_chat_special($id){
		$sql = "SELECT * From ".$this->table('chat_special')." WHERE id=".$id;
		$result=$this->getRow($sql);
		if($result)
		{
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	public function getGroups($id = 0)
	{
		$sql="SELECT * FROM ".$this->table('box_group');
	
		$box_group=$this->getAll($sql);
			
		$sql="SELECT group_id FROM ".$this->table('chat_special') ." where group_id> 0 and id != '".$id."'";
	
		$chat_special=$this->getAll($sql);
			
		foreach ($box_group as $k => $v)
		{
	
			foreach ($chat_special as $kk => $vv)
			{
	
				if($vv['group_id'] == $v['id'])
				{
	
					unset($box_group[$k]);
					//echo "aa";
	
				}
			}
	
		}
			
		if($box_group)
		{
			return $box_group;
		}
		else
		{
			return $result=array();
		}
	}
	
	public function del_chat($id)
	{
		$result=array('error'=>0,'content'=>'删除成功');
		if($id)
		{
	
			$sql="DELETE FROM ".$this->table('chat_special')." WHERE  id='".$id."'";
	
			$res=$this->query($sql);
	
			$sql="DELETE FROM ".$this->table('chat_special_rule')." WHERE  special_id='".$id."'";
	
			$res2=$this->query($sql);
			if(!$res||!$res2)
				// 			if(!$res)
			{
				$result['error']=1;
				$result['content']='删除失败';
			}
		}
		return $result;
	}
	public function getLucky($id,$table="chat_special"){
		$sql="SELECT * FROM ".$this->table('lucky_info')." WHERE status = 1";
		$box_group=$this->getAll($sql);
		if($table=="chat_special"){
			$sql="SELECT hongbao_id FROM ".$this->table('chat_special') ." where hongbao_id> 0 and id != '".$id."'";
			$chat_special3=$this->getAll($sql);
			$sql="SELECT hongbao_id FROM ".$this->table('box') ." where hongbao_id> 0 ";
			$chat_special2=$this->getAll($sql);
			$chat_special = array_merge($chat_special3,$chat_special2);
		}else{
			$sql="SELECT hongbao_id FROM ".$this->table('chat_special') ." where hongbao_id> 0 ";
			$chat_special3=$this->getAll($sql);
			$sql="SELECT hongbao_id FROM ".$this->table('box') ." where hongbao_id> 0 and id != '".$id."'";
			$chat_special2=$this->getAll($sql);
			$chat_special = array_merge($chat_special3,$chat_special2);
		}
		foreach ($box_group as $k => $v)
		{
				
			foreach ($chat_special as $kk => $vv)
			{
					
				if($vv['hongbao_id'] == $v['id'])
				{
					unset($box_group[$k]);
				}
			}
				
		}
	
		if($box_group)
		{
			return $box_group;
		}
		else
		{
			return $result=array();
		}
	}
	
	/***********************************************以下是规则管理的方法****************************************************************/
	public function rule_list($page,$sid){
		$listPage=array('data'=>'','pageLink'=>'');
		$where = " special_id = $sid order by addtime desc ";
		$result=$this->get_page(" * ",$this->table('chat_special_rule'),$where,$page,'20');
		foreach($result as $k=>$v){
			switch ($v['status']){
				case 1:$result[$k]['statusShow'] = "时间规则";break;
				case 2:$result[$k]['statusShow'] = "条数规则";break;
				case 3:$result[$k]['statusShow'] = "每日时间规则";break;
				case 4:$result[$k]['statusShow'] = "单日时间规则";break;
			}
				
		}
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('chat_special_rule')."  where  special_id = $sid ";
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	
	public function get_chat_special_rule($id)
	{
		$sql = "SELECT * From ".$this->table('chat_special_rule')." WHERE id=".$id;
		$result=$this->getRow($sql);
		if($result)
		{
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	
	public function  list_record($page,$cfg=array()){
		$where=" ";
		$logic=" AND ";
		foreach($cfg as $k=>$v)
		{
			if($k=="startTime"){
				$where.=" p1.addtime>=".$v.$logic;
			}elseif ($k=="endTime"){
				$where.=" p1.addtime<=".$v.$logic;
			}elseif($k=="comment"&&$v!=""){
				$where.=" p1.comment like '%".$v."%' ".$logic;
			}elseif($k=="nickname"&&$v!=""){
				$where.=" p2.nickname like '%".$v."%' ".$logic;
			}else{
				switch ($v){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过
					case 0:break;
					case 1:$where.=" p1.status = 0 ".$logic;break;
					case 2:$where.=" p1.status = 1 ".$logic;break;
					case 3:$where.=" p1.status in (3,4) ".$logic;break;
					case 4:$where.=" p1.status in (2,5) ".$logic;break;
					case 5:$where.=" p1.status in(6,7) ".$logic;break;
				}
			}
		}
		$listPage=array('data'=>'','pageLink'=>'');
		$where1 = "$where 1=1 "." order by  p1.addtime desc ";
		$result=$this->get_page2(" p1.* ,  p2.nickname,p2.openid ",$this->table('chat_record')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid=p2.wxid  ",$where1,$page,'50');
		$digestTime = $this->get_cfg('digestTime');
		foreach($result as $k=>$v){
			switch ($v['status']){//审核状态0:未审核;1:审核中;2:审核中不通过；3:审核中通过；4:审核通过 5:审核不通过 6:经过审核未审核7审核中未经过审核的
				case 0:$result[$k]['statusShow'] = "未审核";break;
				case 1:$result[$k]['statusShow'] = "审核中";break;
				case 2:$result[$k]['statusShow'] = "审核不通过";break;
				case 3:$result[$k]['statusShow'] = "审核通过";break;
				case 4:$result[$k]['statusShow'] = "审核通过";break;
				case 5:$result[$k]['statusShow'] = "审核不通过";break;
				case 6:$result[$k]['statusShow'] = "未经过审核";break;
				case 7:$result[$k]['statusShow'] = "未经过审核";break;
				case 8:$result[$k]['statusShow'] = "立即对话";break;
				case 9:$result[$k]['statusShow'] = "立即对话";break;
			}
			//如果该时间戳超过24小时，则失效
			if($v['addStatus']==1){//加精评论
				if(time()-$v['addDigestTime']>($digestTime*86400)){
					$result[$k]['addStatus'] =0;
					$result[$k]['addDigestTime'] =0;
				}
			}
		}
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		if($cfg['nickname']){
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 , ".$this->table('wxusers')."  as p2 where  $where1 ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 , ".$this->table('wxusers')."  as p2 where p1.status in (3,4)  AND $where1   ";
			$pass_count=$this->getOne($sql);
			$listPage['pass_count']=$pass_count;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 , ".$this->table('wxusers')."  as p2 where p1.status in (2,5) AND $where1    ";
			$nopass_count=$this->getOne($sql);
			$listPage['nopass_count']=$nopass_count;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 , ".$this->table('wxusers')."  as p2 where p1.status in (0,1) AND $where1    ";
			$passing_count=$this->getOne($sql);
			$listPage['passing_count']=$passing_count;
		}else{
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where $where  1=1  ";
			$count=$this->getOne($sql);
			$listPage['count']=$count;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where $where  p1.status in (3,4)  ";
			$pass_count=$this->getOne($sql);
			$listPage['pass_count']=$pass_count;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where $where  p1.status in (2,5)  ";
			$nopass_count=$this->getOne($sql);
			$listPage['nopass_count']=$nopass_count;
			$sql="SELECT count(*) FROM ".$this->table('chat_record')."  as p1 where $where  p1.status in (0,1)  ";
			$passing_count=$this->getOne($sql);
			$listPage['passing_count']=$passing_count;
		}
		return $listPage;
	}
}